using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Masood.MobileGames.SlidingLib.Enum;
using Masood.MobileGames.SlidingLib.Abstract;
using Masood.MobileGames.SlidingLib.Board;
using Masood.MobileGames.SlidingLib.Box;
using Masood.MobileGames.SlidingLib.Common;
using Microsoft.Xna.Framework.Graphics;

namespace Masood.MobileGames.SlidingLib.Factory
{
    public class GameBoardFactory
    {
        private static AbstractGameBoard CreateGame(int rows, int cols, System.IO.Stream stream, 
                    Microsoft.Xna.Framework.Graphics.GraphicsDevice graphicsDevice, 
                    Microsoft.Xna.Framework.Content.ContentManager contentManager, 
                    GameEnum level, int actualWidth, int actualHeight)
        {
            AbstractGameBoard board = null;
            int xCount = 0, yCount = 0;
            switch (level)
            {
                case GameEnum.Picture:
                case GameEnum.ForKidApple:                    
                    board = new ImageGameBoard(4, 2, actualWidth, actualHeight);
                    if (stream != null)
                    {
                        board.SourceImageTexture = Helper.ImageStreamToTexture2D(graphicsDevice, stream);
                        board.ImageTextures = Helper.Split(board.SourceImageTexture, board.BoxWidth, board.BoxHeight, out xCount, out yCount).ToList<Texture2D>();
                    }
                    
                    //board.BoxWidth = 100;
                    break;
                case GameEnum.Easy:
                    board = new NumberedGameBoard(4, 2, actualWidth, actualHeight);
                    //board.BoxWidth = 100;
                    break;
                case GameEnum.Medium:
                    board = new NumberedGameBoard(5, 3, actualWidth, actualHeight);
                    //board.BoxWidth = 100;
                    break;
                case GameEnum.Hard:
                    board = new NumberedGameBoard(7, 4, actualWidth, actualHeight);
                    //board.BoxWidth = 80;
                    break;
                case GameEnum.Custom:
                    board = new NumberedGameBoard(rows, cols, actualWidth, actualHeight);
                    break;

                default:
                    throw new ArgumentException("Level not supported.");
            }

            board.Content = contentManager;

            /*
            int midX = actualWidth / 2;
            int midY = actualHeight / 2;
            int resetButtonWidth = 218;
            int resetButtonHeight = 78;
            int buttonPosX = midX - (resetButtonWidth / 2);
            int buttonPosY = actualHeight - 200 + 100;

            buttonPosX = 15;

            board.MusicState = MusicState.Off;
            board.MusicButton = new MusicOffButton(buttonPosX, buttonPosY - 100, 55, 55);
            board.MusicButton.Content = contentManager;            

            board.ResetButton = new ResetButton(buttonPosX, buttonPosY, resetButtonWidth, resetButtonHeight);
            board.ResetButton.Content = contentManager;

            board.PlayButton = new PlayButton(buttonPosX + resetButtonWidth + 15, buttonPosY, resetButtonWidth, resetButtonHeight);
            board.PlayButton.Content = contentManager;
            */            

            return board;
        }

        public static AbstractGameBoard CreateGame(DataObject.GameCreateOption gameCreationOption)
        {
            return CreateGame(gameCreationOption.NoOfRows, 
                                gameCreationOption.NoOfCols, 
                                gameCreationOption.SourceImageStream,
                                gameCreationOption.GraphicsDevice,
                                gameCreationOption.Content,
                                gameCreationOption.GameLevel, 
                                (int)gameCreationOption.ActualWidth, 
                                (int)gameCreationOption.ActualHeight);
        }
    }
}
